package pinboard.dataLayer.potocol.op;

import com.google.gson.Gson;

import java.io.UnsupportedEncodingException;

import pinboard.dataLayer.ByteArrayConvert;
import pinboard.dataLayer.potocol.instructFormat.response.JoinMeetingParam;
import pinboard.dataLayer.potocol.instructFormat.response.KeepAliveParam;
import pinboard.dataLayer.potocol.instructFormat.response.LoginParam;
import pinboard.dataLayer.potocol.instructFormat.Request;
import pinboard.dataLayer.potocol.instructFormat.response.UpdateMeetingStateParam;

public final class InstructClientEncoder {

    static private final int andriodDeviceType = 4;

    public static byte[] login(String sequence, String userId, String password) {
        LoginParam pObj = new LoginParam();
        pObj.deviceType = andriodDeviceType;
        pObj.userId = userId;
        pObj.password = password;
        String method = "Login2";

        byte[] data = encode(pObj, method, sequence);
        return data;
    }

    public static byte[] keepAlive(String sequence, String accessToken) {
        KeepAliveParam pObj = new KeepAliveParam();
        pObj.accessToken = accessToken;
        String method = "KeepAlive";

        byte[] data = encode(pObj, method, sequence);
        return data;
    }

    public static byte[] joinMeeting(String sequence, String accessToken, @Deprecated String userId, String meetingId,
                                     @Deprecated String displayName, String password) {
        JoinMeetingParam pObj = new JoinMeetingParam();
        pObj.accessToken = accessToken;
        pObj.userId = userId;
        pObj.meetingId = meetingId;
        pObj.displayName = displayName;
        pObj.password = password;
        String method = "JoinMeeting";

        byte[] data = encode(pObj, method, sequence);
        return data;
    }

    public static byte[] updateMeetingState(String sequence, UpdateMeetingStateParam param) {
        String method = "UpdateMeetingState";
        byte[] data = encode(param, method, sequence);
        return data;
    }

    private static byte[] encode(Object parameter, String method, String sequence) {

        Gson gson = new Gson();
        String paramJStr = gson.toJson(parameter);

        Request rif = new Request();
        rif.method = method;
        rif.parameter = paramJStr;
        rif.sequence = sequence;

        String jsonStr = gson.toJson(rif);
        try {
            byte[] content = jsonStr.getBytes("utf8");
            byte[] head = ByteArrayConvert.int2Bytes(content.length);
            byte[] result = concat(head, content);
            return result;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }

    private static byte[] concat(byte[] first, byte[] second) {
        byte[] result = new byte[first.length + second.length];
        System.arraycopy(first, 0, result, 0, first.length);
        System.arraycopy(second, 0, result, first.length, second.length);
        return result;
    }

}
